//
//  SSHomeController.m
//  电商项目
//
//  Created by Esther on 2017/2/18.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSHomeController.h"
#import <SDCycleScrollView.h>
#import "SSFocus.h"
#import "SSFirstSell.h"
#import "SSSingleTableViewCell.h"
#import "SSGroupTableCell.h"
#import "SSDetailsController.h"
#import "SSSearchController.h"


@interface SSHomeController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,SDCycleScrollViewDelegate>
@property(nonatomic, strong) UIScrollView *mainScroll;
@property(nonatomic, strong) SDCycleScrollView *headImageView;
@property(nonatomic, strong) UIView *twoButtonView;
@property(nonatomic, strong) UITableView *leftTableView;
@property(nonatomic, strong) UITableView *rightTableView;
@property(nonatomic, strong) UIButton *leftButton;
@property(nonatomic, strong) UIButton *rightButton;
@property(nonatomic, strong) NSArray *focusArr;
@property(nonatomic, strong) NSMutableArray *focusImageArr;
@property(nonatomic, strong) NSArray<SSFirstSell *> *firstSellList;
@property(nonatomic, strong) NSArray<SSFocus *> *groupImageList;

@end

@implementation SSHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.mainScroll];
    [self.mainScroll addSubview:self.headImageView];
   
    [self.mainScroll addSubview:self.leftTableView];
    [self.mainScroll addSubview:self.rightTableView];
    
    [self.mainScroll addSubview:self.twoButtonView];
    [self.twoButtonView addSubview:self.leftButton];
    [self.twoButtonView addSubview:self.rightButton];
    
//    [self requestHeadImage];
    [self requestNew];
    [self requestGroupImage];
    
    [self addSearchButton];
    
    

}


#pragma mark -
#pragma mark - 跳转

- (void)addSearchButton{
    UIButton *searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [searchButton setImage:[UIImage imageNamed:@"限时特卖界面搜索按钮"] forState:(UIControlStateNormal)];
    searchButton.frame = CGRectMake(0, 0, 35, 35);
    [searchButton addTarget:self action:@selector(pushSearchController) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    self.navigationItem.rightBarButtonItem = navItem;
    
    
}


#pragma mark -
#pragma mark - 监听方法
- (void)changeTableViewFrame:(UIButton *)button{
    if (button == self.leftButton) {
        button.selected = YES;
        _rightButton.selected = NO;
        [UIView animateWithDuration:0.25 animations:^{
            CGRect leftTableRect = _leftTableView.frame;
            leftTableRect.origin.x = 0;
            _leftTableView.frame = leftTableRect;
            
            CGRect rightTableRect = _rightTableView.frame;
            rightTableRect.origin.x = [UIScreen ss_screenWidth];
            _rightTableView.frame = rightTableRect;
        }];
        
        CGRect leftTableViewRect = self.leftTableView.frame;
        leftTableViewRect.size.height = self.firstSellList.count * 170;
        self.leftTableView.frame = leftTableViewRect;
        if (self.leftButton.selected) {
            self.mainScroll.contentSize = CGSizeMake(0, self.firstSellList.count  * 170 + 280 + 64);
        }
        
        
    }else{
        button.selected = YES;
        _leftButton.selected = NO;
        [UIView animateWithDuration:0.25 animations:^{
            CGRect leftTableRect = _leftTableView.frame;
            leftTableRect.origin.x = - [UIScreen ss_screenWidth];
            _leftTableView.frame = leftTableRect;
            
            CGRect rightTableRect = _rightTableView.frame;
            rightTableRect.origin.x = 0;
            _rightTableView.frame = rightTableRect;
        }];
        CGRect rightTableViewRect = self.rightTableView.frame;
        rightTableViewRect.size.height = self.groupImageList.count * 200;
        //    if (self.rightButton.selected) {
        
        self.mainScroll.contentSize = CGSizeMake(0, self.groupImageList.count * 200 + 280 + 64);
        [self.rightTableView layoutIfNeeded];
        //    }
        
       
    }
}

- (void)pushSearchController{
    SSSearchController *searchController = [[SSSearchController alloc] init];
    
    [self.navigationController pushViewController:searchController animated:YES];
    
}
#pragma mark -
#pragma mark - delegate && dataSoure
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTableView) {
        return self.firstSellList.count;
    }else{
        return self.groupImageList.count;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.leftTableView) {
        SSSingleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"singleCell"];
        if (!cell) {
            cell = [[SSSingleTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"singleCell"];
        }
        
        cell.firstSell = self.firstSellList[indexPath.row];
        
        return cell;
        
    }else{
        SSGroupTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupImageCell"];
        if (!cell) {
            cell = [[SSGroupTableCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"groupImageCell"];
            
        }
        
        cell.groupImageString = self.groupImageList[indexPath.row];
        
        return cell;
        
    }


    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        return 170.0;
    }else{
        return 200.0;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%zd",indexPath.row);
    SSDetailsController *detailsController = [[SSDetailsController alloc] init];
    detailsController.firstSel = self.firstSellList[indexPath.row];
    
    [self.navigationController pushViewController:detailsController animated:YES];
    
    
}

#pragma mark -
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y > 230) {
        CGRect rect = _twoButtonView.bounds;
        rect.origin.y = scrollView.contentOffset.y;
        _twoButtonView.frame = rect;
    }else{
        CGRect rect = _twoButtonView.bounds;
        rect.origin.y = 230;
        _twoButtonView.frame = rect;
    }
}

#pragma mark -
#pragma mark - 网络数据加载
- (void)requestHeadImage{

    [[SSNetworkTools shared] ss_getLoadData:@"appHome/appHome.do" completeBlock:^(id responseObject, NSError *error) {
        if (error != nil) {
            return;
        }
        NSLog(@"%@",responseObject);
        
    }];
    
    
}

- (void)requestNew{
    [SSFirstSell loadFirstSellData:^(NSArray<SSFirstSell *> *data) {
        self.firstSellList = data;
    }];
}

- (void)requestGroupImage{
    [SSFocus loadFocusData:^(NSArray<SSFocus *> *data) {
        self.groupImageList = data;
    }];
}

- (void)setFirstSellList:(NSArray<SSFirstSell *> *)firstSellList{
    _firstSellList = firstSellList;
    
    CGRect leftTableViewRect = self.leftTableView.frame;
    leftTableViewRect.size.height = self.firstSellList.count * 170;
    self.leftTableView.frame = leftTableViewRect;
    if (self.leftButton.selected) {
        self.mainScroll.contentSize = CGSizeMake(0, self.firstSellList.count  * 170 + 280 + 64);
    }
    
    [self.leftTableView reloadData];
    
    
}
- (void)setGroupImageList:(NSArray<SSFocus *> *)groupImageList{
    _groupImageList = groupImageList;
    CGRect rightTableViewRect = self.rightTableView.frame;
    rightTableViewRect.size.height = self.groupImageList.count * 200;
//    if (self.rightButton.selected) {
    
        self.mainScroll.contentSize = CGSizeMake(0, self.groupImageList.count * 200 + 280 + 64);
//        [self.rightTableView layoutIfNeeded];
//    }

    [self.rightTableView reloadData];
    
}

#pragma mark -
#pragma mark - 懒加载
- (UIScrollView *)mainScroll{
    if (!_mainScroll) {
        _mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen ss_screenWidth], [UIScreen ss_screenHeight] - 64)];
        _mainScroll.delegate = self;
    }
    return _mainScroll;
}

- (SDCycleScrollView *)headImageView{
    if (!_headImageView) {
        // 网络加载 --- 创建带标题的图片轮播器
        _headImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen ss_screenWidth], 230) delegate:self placeholderImage:[UIImage imageNamed:@"icomImage"]];
        //[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen ss_screenWidth], 230)];
        
        // >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        
        _headImageView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _headImageView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
   
        [SSFocus loadFocusData:^(NSArray<SSFocus *> *data) {
            for (SSFocus *focus in data) {
                [self.focusImageArr addObject:focus.img];
                
            }
            _headImageView.imageURLStringsGroup = self.focusImageArr.copy;
        }];
    }
    return _headImageView;
}

- (UIView *)twoButtonView{
    if (!_twoButtonView) {
        _twoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 230, [UIScreen ss_screenWidth], 50)];
        _twoButtonView.backgroundColor = [UIColor whiteColor];
    }
    return _twoButtonView;
    
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _leftButton.frame = CGRectMake(0, 0, [UIScreen ss_screenWidth] * 0.5, 50);
        [_leftButton addTarget:self action:@selector(changeTableViewFrame:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_leftButton setTitle:@"新品团购" forState:(UIControlStateNormal)];
        [_leftButton setImage:[UIImage imageNamed:@"限时特卖界面新品团购图标未选中"] forState:(UIControlStateNormal)];
        [_leftButton setImage:[UIImage imageNamed:@"限时特卖界面新品团购图标"] forState:(UIControlStateSelected)];
        [_leftButton setTitleColor:[UIColor ss_colorWithRed:56 green:166 blue:243] forState:(UIControlStateNormal)];
        [_leftButton setTitleColor:[UIColor ss_colorWithRed:213 green:48 blue:34] forState:(UIControlStateSelected)];
        _leftButton.selected = YES;
    }
    return _leftButton;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _rightButton.frame = CGRectMake([UIScreen ss_screenWidth] * 0.5, 0, [UIScreen ss_screenWidth] * 0.5, 50);
        [_rightButton addTarget:self action:@selector(changeTableViewFrame:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_rightButton setTitle:@"品牌团购" forState:(UIControlStateNormal)];
        [_rightButton setImage:[UIImage imageNamed:@"限时特卖界面品牌团购图标"] forState:(UIControlStateNormal)];
        [_rightButton setImage:[UIImage imageNamed:@"限时特卖界面品牌团购图标选中"] forState:(UIControlStateSelected)];
        [_rightButton setTitleColor:[UIColor ss_colorWithRed:56 green:166 blue:243] forState:(UIControlStateNormal)];
        [_rightButton setTitleColor:[UIColor ss_colorWithRed:213 green:48 blue:34] forState:(UIControlStateSelected)];
        _rightButton.selected = NO;
        
    }
    return _rightButton;
}

- (UITableView *)leftTableView{
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 280, [UIScreen ss_screenWidth], 1700) style:(UITableViewStylePlain)];//[UIScreen ss_screenHeight]
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        _leftTableView.bounces = NO;
    }
    return _leftTableView;
}

- (UITableView *)rightTableView{
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake([UIScreen ss_screenWidth], 280, [UIScreen ss_screenWidth], 1700) style:(UITableViewStylePlain)];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.bounces = NO;
    }
    return _rightTableView;
}

- (NSArray *)focusArr{
    if (!_focusArr) {
        _focusArr = [NSArray array];
       
        
    }
    return _focusArr;
}
- (NSMutableArray *)focusImageArr{
    if (!_focusImageArr) {
        _focusImageArr = [NSMutableArray array];
        
    }
    return _focusImageArr;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
